﻿using System;
using System.Collections.Generic;
using System.Linq;
using InvestmentIntelligence.DbModel.Models;

namespace InvestmentIntelligence.Data.Repository.Comparers.EntityModelsComparers
{
    public sealed class SecurityEqualityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            if (ReferenceEquals(x, y)) return true;
            if (ReferenceEquals(x, null)) return false;
            if (ReferenceEquals(y, null)) return false;
            var retBool =  string.Equals(x.Isin, y.Isin) &&
                string.Equals(x.Ric, y.Ric) &&
                string.Equals(x.Sedol, y.Sedol) &&
                string.Equals(x.CustomIdentificator, y.CustomIdentificator) &&
                x.ClassId == y.ClassId &&
                x.CountryId == y.CountryId &&
                x.CurrencyId == y.CurrencyId &&
                x.EconomicSectorId == y.EconomicSectorId &&
                string.Equals(x.Name, y.Name) &&
                string.Equals(x.LongName, y.LongName) &&
                string.Equals(x.Mic, y.Mic) &&
                x.Type == y.Type &&
                string.Equals(x.ParentIdentify, y.ParentIdentify) &&
                string.Equals(x.ParentIdentifyType, y.ParentIdentifyType) &&
                string.Equals(x.Ticker, y.Ticker);
            return retBool;
        }

        public int GetHashCode(Security obj)
        {
            return 1;
        }
    }

    public sealed class SecurityIdentitesEqualityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            if (ReferenceEquals(x, y)) return true;
            if (ReferenceEquals(x, null)) return false;
            if (ReferenceEquals(y, null)) return false;
            return
                string.Equals(x.Isin, y.Isin) &&
                string.Equals(x.Ric, y.Ric) &&
                string.Equals(x.Sedol, y.Sedol) &&
                string.Equals(x.CustomIdentificator, y.CustomIdentificator) &&
                string.Equals(x.Mic, y.Mic) &&
                string.Equals(x.Ticker, y.Ticker);
        }

        public int GetHashCode(Security obj)
        {
            unchecked
            {
                int hashCode = obj.Id;
                return hashCode;
            }
        }
    }


    public sealed class DefaultSecurityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            return string.Equals(x.Isin, y.Isin, StringComparison.InvariantCultureIgnoreCase)
                && string.Equals(x.Ric, y.Ric, StringComparison.InvariantCultureIgnoreCase)
                 && string.Equals(x.Sedol, y.Sedol, StringComparison.InvariantCultureIgnoreCase)
                 && string.Equals(x.Ticker, y.Ticker, StringComparison.InvariantCultureIgnoreCase)
                 && string.Equals(x.Mic, y.Mic, StringComparison.InvariantCultureIgnoreCase)
                ;
        }

        public int GetHashCode(Security obj)
        {
            return 1;
        }
    }


    public sealed class IsinSecurityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            return string.Equals(x.Isin, y.Isin, StringComparison.InvariantCultureIgnoreCase);
        }

        public int GetHashCode(Security obj)
        {
            return 1;
        }
    }

    public sealed class SedolSecurityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            return string.Equals(x.Sedol, y.Sedol, StringComparison.InvariantCultureIgnoreCase);
        }

        public int GetHashCode(Security obj)
        {
            return 1;
        }
    }

    public sealed class RicSecurityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            return string.Equals(x.Ric, y.Ric, StringComparison.InvariantCultureIgnoreCase);
        }

        public int GetHashCode(Security obj)
        {
            return 1;
        }
    }

    public sealed class MicSecurityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            return string.Equals(x.Mic, y.Mic, StringComparison.InvariantCultureIgnoreCase);
        }

        public int GetHashCode(Security obj)
        {
            return 1;
        }
    }

    public sealed class CustomIdentificatorSecurityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            return string.Equals(x.CustomIdentificator, y.CustomIdentificator, StringComparison.InvariantCultureIgnoreCase);
        }

        public int GetHashCode(Security obj)
        {
            return 1;
        }
    }

    public sealed class TickerSecurityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            return string.Equals(x.Ticker, y.Ticker, StringComparison.InvariantCultureIgnoreCase);
        }

        public int GetHashCode(Security obj)
        {
            return 1;
        }
    }

    public sealed class ArraySecurityComparer : IEqualityComparer<Security>
    {
        public bool Equals(Security x, Security y)
        {
            var array1 = new List<string>
                {
                    string.IsNullOrWhiteSpace(x.Isin) ?null: "I:" + x.Isin,
                    string.IsNullOrWhiteSpace(x.Ric) ?null: "R:" + x.Ric,
                    string.IsNullOrWhiteSpace(x.Mic) ?null: "M:" + x.Mic,
                    string.IsNullOrWhiteSpace(x.Sedol) ?null: "S:" + x.Sedol,
                    string.IsNullOrWhiteSpace(x.Ticker) ?null: "T:" + x.Ticker,
                    string.IsNullOrWhiteSpace(x.CustomIdentificator) ?null: "C:" + x.CustomIdentificator,
                };
            var array2 = new List<string>
                {
                    string.IsNullOrWhiteSpace(y.Isin) ?null: "I:" + y.Isin,
                    string.IsNullOrWhiteSpace(y.Ric) ?null: "R:" + y.Ric,
                    string.IsNullOrWhiteSpace(y.Mic) ?null: "M:" + y.Mic,
                    string.IsNullOrWhiteSpace(y.Sedol) ?null: "S:" + y.Sedol,
                    string.IsNullOrWhiteSpace(y.Ticker) ?null: "T:" + y.Ticker,
                    string.IsNullOrWhiteSpace(y.CustomIdentificator) ?null: "C:" + y.CustomIdentificator,
                };
            array1 = array1.Where(z => z != null).ToList();
            array2 = array2.Where(z => z != null).ToList();
            return  array1.Intersect(array2).Any();
        }

        public int GetHashCode(Security obj)
        {
            return 1;
        }
    }
}
